package com.jd.fruit.controller.admin.auth;

import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.common.util.MD5;
import com.jd.fruit.common.util.WebContextUtil;
import com.jd.fruit.model.admin.auth.AdminUser;
import com.jfinal.kit.StrKit;

/**
 * 管理员
 * 
 * @author lishunfeng
 * 
 */
@Controller("/admin")
public class LoginController extends BaseController {
	/**
	 * 登陆首页
	 */
	public void login() {
		render("login.html");
	}
	/**
	 * 跳转无权限
	 */
	public void noAuthority() {
		render("noAuthority.html");
	}

	/**
	 * 验证登陆信息
	 */
	public void checkLogin() {
		String loginName = this.getPara("loginName");
		String password = this.getPara("password");
		if (StrKit.isBlank(loginName) || StrKit.isBlank(password)) {
			this.redirect("/admin/login");
		} else {
			password = MD5.md5(password);
			AdminUser adminuser = AdminUser.dao.checkLogin(loginName, password);
			if (adminuser != null) {
				// 清楚已经存在的session
				WebContextUtil.logout(this.getRequest());
				this.setSessionAttr("loginUserId", adminuser.getStr("id"));
				this.setSessionAttr("loginUser", adminuser);
			    this.redirect("/admin/auth/user/list");
			} else {
				//this.redirect("/admin/login");
				this.redirect("/admin/auth/user/list");
			}
		}
	}

	/**
	 * 退出
	 */
	public void logout() {
		WebContextUtil.logout(this.getRequest());
		this.redirect("/admin/login");
	}
}
